package com.daxia.ogmall.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.HouseStyleDAO;
import com.daxia.ogmall.dto.BrandDTO;
import com.daxia.ogmall.dto.BrandStyleDTO;
import com.daxia.ogmall.dto.HouseStyleDTO;
import com.daxia.ogmall.model.HouseStyle;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class HouseStyleService {
	
	@Autowired
	private HouseStyleDAO houseStyleDAO;
	
	public List<HouseStyleDTO> findDistinctStyleFromBrand(BrandStyleDTO query) {   //拎包入住
		List<HouseStyle> styles = houseStyleDAO.findDistinctStyleFromBrand(query);
		List<HouseStyleDTO> dtos = toDTOs(styles);
		return dtos;
	}
	
	public List<HouseStyleDTO> find(HouseStyleDTO query, Page page) {
		List<HouseStyle> models = houseStyleDAO.find(query, page);
		List<HouseStyleDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<HouseStyleDTO> findAll() {
		return this.find(new HouseStyleDTO(), null);
	}
	
	public List<HouseStyleDTO> findBrandHouseStyle(){
		return houseStyleDAO.findBrandHouseStyle();
	}
	public List<HouseStyleDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new HouseStyleDTO(), page);
	}
	
	public Long create(HouseStyleDTO dto) {
		HouseStyle model = new HouseStyle();
		toModel(model, dto);
		return houseStyleDAO.create(model);
	}
	
	public HouseStyleDTO load(Long id) {
	    HouseStyle model = houseStyleDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(HouseStyleDTO dto) {
		HouseStyle model = houseStyleDAO.load(dto.getId());
		toModel(model, dto);
		houseStyleDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				houseStyleDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public HouseStyleDTO findOne(HouseStyleDTO query) {
		HouseStyle model = houseStyleDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<HouseStyleDTO> toDTOs(List<HouseStyle> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<HouseStyleDTO>(0);
		}
		List<HouseStyleDTO> dtos = new ArrayList<HouseStyleDTO>(models.size());
		for (HouseStyle model : models) {
	        HouseStyleDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private HouseStyleDTO toDTO(HouseStyle model) {
		if (model == null) {
			return null;
		}
		HouseStyleDTO dto = BeanMapper.map(model, HouseStyleDTO.class);
		
		return dto;
	}
	
	private void toModel(HouseStyle model, HouseStyleDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<HouseStyle> toModels(List<HouseStyleDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<HouseStyle>(0);
		}
		List<HouseStyle> models = new ArrayList<HouseStyle>(dtos.size());
		for (HouseStyleDTO dto : dtos) {
	        HouseStyle model = new HouseStyle();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
